package com.example.test.dialogfactory;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;
import easier.app.x.view.GridLayoutSpaceInDecoration;
import easier.log.logger.Logger;
import easier.recycler.DataViewHolder;
import easier.recycler.SingleTypeAdapter;
import java.util.ArrayList;
import java.util.List;

public class MainActivity21 extends AppCompatActivity {

    private RecyclerView mRecycler;

    public static void start( Context context ) {

        Intent starter = new Intent( context, MainActivity21.class );
        context.startActivity( starter );
    }

    @Override
    protected void onCreate( Bundle savedInstanceState ) {

        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main21 );
        initView();
    }

    private void initView() {

        int spanCount = 5;

        mRecycler = (RecyclerView) findViewById( R.id.recycler );
        mRecycler.setLayoutManager( new GridLayoutManager( this, spanCount ) );
        SingleTypeAdapter< String > adapter = new SingleTypeAdapter< String >() {
            @Override
            protected DataViewHolder< String > buildHolder( int position, String o ) {

                return new DataViewHolder<>( R.layout.activity_main_21_item, o ) {
                    @Override
                    protected void onBind( ViewHolder viewHolder, View root, int position, int type, List< Object > payloads ) {

                        root.post( new Runnable() {
                            @Override
                            public void run() {

                                Logger.d( position + " " + root );
                            }
                        } );
                    }
                };
            }
        };
        mRecycler.setAdapter( adapter );

        List< String > data = new ArrayList<>();
        for ( int i = 0; i < spanCount * 3; i++ ) {
            data.add( "" );
        }
        adapter.updateSource( data );

        GridLayoutSpaceInDecoration decoration = new GridLayoutSpaceInDecoration( spanCount, 20 );
        mRecycler.addItemDecoration( decoration );
    }
}